Vernam Cipher (ASCII)

Module 01 / Lesson 09

Video Tutorial


The Power of XOR and ASCII

The Vernam cipher is a stream cipher that uses the XOR (Exclusive OR) logical operation. In the modern version, we convert characters to their ASCII binary values before applying the key.

ASCII Reference Sample:

CharDecimalBinary
A6501000001
B6601000010
C6701000011

* Values based on the standard ASCII table.

Key Requirements for Perfect Security:

  • The key must be as long as the message.
  • The key must be truly random.
  • The key must be used only once (One-Time Pad).

Python Implementation (XOR Logic)

def vernam_xor(text, key):
    result = ""
    # Each character is XORed with the corresponding key character
    for t_char, k_char in zip(text, key):
        # ord() gets ASCII decimal, ^ is XOR, chr() converts back
        xor_val = ord(t_char) ^ ord(k_char)
        result += chr(xor_val)
    return result

# Usage
message = "HELLO"
key = "XMCKL" # Key must be same length
encrypted = vernam_xor(message, key)
print(f"Encrypted: {encrypted.encode()}")